home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / PowerD / alpha / examples / Talk.d < prev    next >
Encoding:
Text File  |  2002-10-28  |  5.5 KB  |  150 lines

  1. /* Talk V1.0 - by Rob Verver in 1992; Translated to PowerD by DMX in 2002  */
  2. /*                                                                         */
  3. /* With this shellcommand you can make the narrator say any text using the */
  4. /* new OS2 features. See the helptemplate for more info. When specifying   */
  5. /* a value which is out of range, the correct range will be displayed.     */
  6. /*                                                                         */
  7. /* Possible enhancements:                                                  */
  8. /*   Ability to speak phonetic strings                                     */
  9. /*   Input from standard input, for piping                                 */
  10. /*   Preferences file in ascii format, controling all settings             */
  11. /*   Escape codes changes values halfway a text                            */
  12. OPT DOSONLY,OPTIMIZE=3
  13.  
  14. MODULE 'devices/narrator','dos/dos','exec/memory','exec/io','translator'
  15.  
  16. CONST AUDIOCHANSIZE=4
  17. ENUM NONE, ERR_DOS, ERR_MEM, ERR_FILE, ERR_DEVICE, ERR_TRANS, ERR_INVALID
  18.  
  19. OBJECT arglist
  20.   file, rate, pitch, robotic, female, volume, enthusiasm, perturb, f1adj,
  21.   f2adj, f3adj, a1adj, a2adj, a3adj, articulate, centralize, centphon, avbias,
  22.   afbias, priority
  23. ENDOBJECT
  24.  
  25. DEF template, phonebuf=NIL, rdargs=NIL, file=NIL, msgport=NIL,
  26.     filebuf=NIL, ioreq:PTR TO NDI, audiochan: PTR TO CHAR, valid=TRUE,
  27.     length, phonebufsize,TranslatorBase
  28.  
  29. PROC main()
  30.   audiochan := [3, 5, 10, 12]:CHAR
  31.  
  32.   /* parse commandline options */
  33.   DEF args = [NIL, [DEFRATE], [DEFPITCH], 0, 0, [DEFVOL], [DEFF0ENTHUS],
  34.           [DEFF0PERT], [0], [0], [0], [0], [0], [0], [DEFARTIC],
  35.           [DEFCENTRAL], NIL, [0], [0], [25]]:arglist
  36.   template := 'FILE,RATE/K/N,PITCH/K/N,ROBOTIC/S,FEMALE/S,VOLUME/K/N,' + 
  37.               'ENTHUSIASM/K/N,PERTURB/K/N,F1ADJ/K/N,F2ADJ/K/N,F3ADJ/K/N,' +
  38.               'A1ADJ/K/N,A2ADJ/K/N,A3ADJ/K/N,ARTICULATE/K/N,CENTRALIZE/K/N,' +
  39.               'CENTPHON/K,AVBIAS/K/N,AFBIAS/K/N,PRIORITY/K/N'
  40.   rdargs := ReadArgs (template, args, NIL)
  41.   IF rdargs=NIL THEN Raise (ERR_DOS)
  42.  
  43.   /* open translator library */
  44.   TranslatorBase := OpenLibrary ('translator.library', 37)
  45.   IF TranslatorBase=NIL THEN Raise (ERR_TRANS)
  46.  
  47.   /* open input file */
  48.   file := Open (args.file, MODE_OLDFILE)
  49.   IF file=NIL THEN Raise (ERR_FILE)
  50.   length := FileLength (args.file)           /* !!! ascii, no fh */
  51.   IF length<1 THEN Raise (ERR_FILE)
  52.  
  53.   /* allocate input buffer */
  54.   filebuf := AllocVec (length, MEMF_PUBLIC)
  55.   IF filebuf=NIL THEN Raise (ERR_MEM)
  56.  
  57.   /* allocate buffer for phonetic strings */
  58.   phonebufsize := Shl (length, 1)
  59.   phonebuf := AllocVec (phonebufsize, MEMF_PUBLIC)
  60.   IF phonebuf=NIL THEN Raise (ERR_MEM)
  61.  
  62.   /* open narrator device */
  63.   msgport := CreateMsgPort ()
  64.   IF msgport=NIL THEN Raise (ERR_DEVICE)
  65.   ioreq := CreateIORequest (msgport, SIZEOF_NDI)
  66.   IF ioreq=NIL THEN Raise (ERR_DEVICE)
  67.   ioreq.flags := NDF_NEWIORB
  68.   IFN OpenDevice ('narrator.device', 0, ioreq, NIL)=NIL THEN Raise (ERR_DEVICE)
  69.  
  70.   /* check values validity */
  71.   checkVal (Long (args.rate), MINRATE, MAXRATE, 'Invalid rate')
  72.   checkVal (Long (args.pitch), MINPITCH, MAXPITCH, 'Invalid pitch')
  73.   checkVal (Long (args.volume), MINVOL, MAXVOL, 'Invalid volume')
  74.   checkVal (Long (args.centralize), MINCENT, MAXCENT, 'Invalid centralization')
  75.   IF valid=FALSE THEN Raise (ERR_INVALID)
  76.  
  77.   ioreq.ChMasks := audiochan
  78.   ioreq.NumMasks := AUDIOCHANSIZE
  79.  
  80.   /* init values */
  81.   ioreq.rate := Long (args.rate)
  82.   ioreq.pitch := Long (args.pitch)
  83.   ioreq.volume := Long (args.volume)
  84.   ioreq.F0enthusiasm := Long (args.enthusiasm)
  85.   ioreq.F0perturb := Long (args.perturb)
  86.   ioreq.F1adj := Long (args.f1adj)
  87.   ioreq.F2adj := Long (args.f2adj)
  88.   ioreq.F3adj := Long (args.f3adj)
  89.   ioreq.A1adj := Long (args.a1adj)
  90.   ioreq.A2adj := Long (args.a2adj)
  91.   ioreq.A3adj := Long (args.a3adj)
  92.   ioreq.articulate := Long (args.articulate)
  93.   ioreq.centralize := Long (args.centralize)
  94.   ioreq.centphon := Long (args.centphon)
  95.   ioreq.AVbias := Long (args.avbias)
  96.   ioreq.AFbias := Long (args.afbias)
  97.   ioreq.priority := Long (args.priority)
  98.   IFN args.robotic=NIL THEN ioreq.mode := ROBOTICF0 ELSE ioreq.mode := MANUALF0
  99.   IFN args.female=NIL THEN ioreq.sex := FEMALE
  100.  
  101.   process ()
  102.  
  103.   Raise (0)
  104. EXCEPT
  105.   SELECT exception
  106.     CASE ERR_DOS;     PrintFault (IOErr(), 'Error')
  107.     CASE ERR_MEM;     PutStr ('Error: not enough memory\n')
  108.     CASE ERR_FILE;    PutStr ('Error: couldn\at open file\n')
  109.     CASE ERR_DEVICE;  PutStr ('Error: couldn\at open narrator device\n')
  110.     CASE ERR_TRANS;   PutStr ('Error: could\at open translator library V37\n')
  111.     CASE ERR_INVALID; PutStr ('Error: wrong parameters\n')
  112.   ENDSELECT
  113.  
  114.   IFN ioreq=NIL THEN CloseDevice (ioreq); DeleteIORequest (ioreq)
  115.   IFN TranslatorBase=NIL THEN CloseLibrary (TranslatorBase)
  116.   IFN rdargs=NIL THEN FreeArgs (rdargs)
  117.   IFN phonebuf=NIL THEN FreeVec (phonebuf)
  118.   IFN filebuf=NIL THEN FreeVec (filebuf)
  119.   IFN file=NIL THEN Close (file)
  120.   IFN msgport=NIL THEN DeleteMsgPort (msgport)
  121.   IF exception THEN Exit(10)
  122. ENDPROC
  123.  
  124. PROC checkVal (val, min, max, str)
  125.   IF val<min OR (val>max)
  126.     PrintF('\s: valid values are between \d and \d\n', str, min, max)
  127.     valid := FALSE
  128.   ENDIF
  129. ENDPROC
  130.  
  131. PROC process ()
  132.   DEF readlen          /* !!!! was equal to globvar */
  133.  
  134.   readlen := Read(file, filebuf, length)
  135.   IFN readlen=length THEN Raise (ERR_FILE)
  136.  
  137.   Translate (filebuf, length, phonebuf, phonebufsize)
  138.   /* WriteF ('phonetic string:\s\n', phonebuf) */
  139.   speakBuffer (phonebuf, StrLen (phonebuf))
  140. ENDPROC
  141.  
  142. PROC speakBuffer(buffer, length)
  143.   DEF ior:PTR TO IOStd
  144.   ior := ioreq
  145.   ior.Command := CMD_WRITE
  146.   ior.Data := buffer
  147.   ior.Length := length
  148.   DoIO (ioreq)
  149. ENDPROC
  150.